package com.kordar.api.modular.unicomJS;

import com.alibaba.fastjson.JSONObject;
import com.kordar.api.exception.ErrorType;
import com.kordar.api.exception.ServiceException;
import com.kordar.api.util.AesUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

@Slf4j
@Component
public class ZOPUtil {

    public static JSONObject send(RestTemplate restTemplate, Request request) {

        // 请求头信息
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json");
        headers.add("Accept", "application/json");

        // 请求体内容
        JSONObject baseObj = new JSONObject();
        JSONObject requestInfo = new JSONObject();

        // head
        JSONObject headObj = request.head();
        requestInfo.put("head", headObj);
        // body
        JSONObject bodyObj = request.body();
        requestInfo.put("body", bodyObj);
        log.info(requestInfo.toString());

        baseObj.put("appCode", SysParams.SYS_APP_CODE.getName());
        baseObj.put("requestInfo", AesUtil.encrypt(requestInfo.toString(), SysParams.SYS_SECRET_KEY.getName()));

        // 组装请求信息
        HttpEntity<JSONObject> httpEntity = new HttpEntity<>(baseObj, headers);

        JSONObject result = restTemplate.postForObject(request.url(), httpEntity, JSONObject.class);
        if (result == null) {
            throw new ServiceException(ErrorType.RESULT_NOT_EXIST);
        }

        String responseInfo = result.getString("responseInfo");
        // 解密验签
        String decrypt = AesUtil.decrypt(responseInfo, SysParams.SYS_SECRET_KEY.getName());
        JSONObject responseJson = JSONObject.parseObject(decrypt);
        log.info(responseJson.toJSONString());
        JSONObject head = responseJson.getJSONObject("head");
        JSONObject body = responseJson.getJSONObject("body");
        String _timestamp = head.getString("timestamp");
        String _uuid = head.getString("uuid");
        String _sign = head.getString("sign");
        String $sign = SysParams.getSign(_timestamp, _uuid);
        if (!$sign.equals(_sign)) {
           throw new ServiceException(ErrorType.SING_ERROR);
        }

        return body;
    }

    public static JSONObject getData(JSONObject jsonObject) {
        if (!jsonObject.getString("RSP_CODE").equals("0000")) {
            throw new ServiceException(ErrorType.RSP_CODE_ERROR);
        }
        return jsonObject.getJSONObject("DATA");
    }

    public static String getRandomString(int length) {
        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        StringBuilder sb = new StringBuilder();

        for(int i = 0; i < length; ++i) {
            int number = random.nextInt(62);
            sb.append(str.charAt(number));
        }

        return sb.toString();
    }
}
